	package com.avocado.base.service.impl;
	
	import java.util.ArrayList;

	import java.util.List;

	import com.avocado.base.dto.resources.ResourcesAddDto;
	import com.avocado.base.dto.resources.ResourcesEditDto;
	import com.avocado.base.entity.*;
	import com.avocado.base.vo.ResourcesAntdTree;
	import org.springframework.beans.BeanUtils;
	import org.springframework.stereotype.Service;
	import org.springframework.transaction.annotation.Propagation;
	import org.springframework.transaction.annotation.Transactional;
	import com.avocado.base.enums.ResourcesType;
	import com.avocado.base.repository.ResourcesRepository;
	import com.avocado.base.service.ResourcesService;


    @Service
	public class ResourcesServiceImpl implements ResourcesService {

		private final ResourcesRepository resourcesRepository;


		public ResourcesServiceImpl(ResourcesRepository resourcesRepository) {

			this.resourcesRepository = resourcesRepository;
		}

		@Override
		@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
		public Long add(ResourcesAddDto t) {

			Resources resources=new Resources();

			BeanUtils.copyProperties(t,resources);

			resources.setType(ResourcesType.valueOf(t.getType()));

	    	resourcesRepository.saveAndFlush(resources);

			return resources.getId() ;
		}
		
		@Override
		@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
	    public void update(ResourcesEditDto t) {

	      	Resources oldResources=resourcesRepository.getOne(t.getId());

			oldResources.setType(ResourcesType.valueOf(t.getType()));

			BeanUtils.copyProperties(t,oldResources);

			resourcesRepository.saveAndFlush(oldResources);

		}

		
		@Override
	    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
	    public Boolean deleteById(Long id) {

			Resources old=resourcesRepository.getOne(id);

			long totalCount=resourcesRepository.getCountByParentId(id);
			
			if(totalCount>0L) {

				return Boolean.FALSE;
	    	}

			resourcesRepository.delete(old);

			return Boolean.TRUE;
		}

		@Override
		public List<ResourcesAntdTree> getTree(ResourcesType resourcesType) {

			List<Resources> resourcesList=resourcesRepository.findAll(resourcesType);

			return convertList(resourcesList,0L);
		}

		@Override
		public List<ResourcesAntdTree> getTree(ResourcesType resourcesType, String userName) {

			List<Resources> resourcesList=resourcesRepository.findAll(resourcesType,userName);

			return convertList(resourcesList,0L);
		}


		private List<ResourcesAntdTree> convertList(List<Resources> resourcesList, long parentId){

			List<ResourcesAntdTree> treeList=new ArrayList<>();

			for(Resources resources:resourcesList){

				if(resources.getParentId()==parentId){

					ResourcesAntdTree organizationAntdTree=ResourcesAntdTree.builder().id(resources.getId())
							.key("resources_key_".concat(String.valueOf(resources.getId())))
							.title(resources.getName())
							.parentId(resources.getParentId())
							.code(resources.getCode())
							.originTitle(resources.getName())
							.sortOrder(resources.getSortOrder())
							.build();

					organizationAntdTree.setChildren(this.convertList(resourcesList,resources.getId()));

					treeList.add(organizationAntdTree);
				}

			}

			return treeList;
		}



		public List<ResourcesAntdTree> getCheckBoxTree(ResourcesType resourcesType,Long roleId){

			List<Resources> resourcesList=resourcesRepository.findAll(resourcesType);

			List<Resources> roleResourceslist=resourcesRepository.findAll(resourcesType,roleId);

			return  convertList(resourcesList, roleResourceslist,0);

		}


		private List<ResourcesAntdTree> convertList(List<Resources> resourcesList,List<Resources> roleResourceslist, long parentId){

			List<ResourcesAntdTree> treeList=new ArrayList<>();

			for(Resources resources:resourcesList){

				ResourcesAntdTree organizationAntdTree=ResourcesAntdTree.builder().id(resources.getId())
						.key("resources_key_".concat(String.valueOf(resources.getId())))
						.title(resources.getName())
						.parentId(resources.getParentId())
						.code(resources.getCode())
						.originTitle(resources.getName())
						.sortOrder(resources.getSortOrder())
						.checked(checkExist(resources.getId(),roleResourceslist))
						.build();

				if(resources.getParentId()==parentId){

					organizationAntdTree.setChildren(this.convertList(resourcesList,roleResourceslist,resources.getId()));

					treeList.add(organizationAntdTree);
				}

			}

			return treeList;
		}

		private Boolean checkExist(long resourcesId,List<Resources> roleResourceslist){
			for(Resources t:roleResourceslist){
				if(t.getId()==resourcesId){
					return true;
				}
			}
			return false;
		}







	}
